package com.joinway.framework.data

import java.lang.reflect.Field

import com.joinway.framework.bean.BaseBean

@Deprecated
class DomainEntity extends BaseBean implements GroovyInterceptable, Serializable {

	private Field[] fields
	
	public DomainEntity(){
		fields = this.getClass().getDeclaredFields()
	}

	def invokeMethod(String name, Object args){
		def self = this
		def method = {
			metaClass.getMetaMethod(name, args)?.invoke(self, args)
		}
		
		if(fields && name.substring(0, 3).equals("get")){
			String prop = name.substring(3)
			prop = prop.substring(0, 1).toLowerCase() + prop.substring(1)
			
			Field target = fields.find(){it.getName().equals(prop)}
			if(!target){
				return method.call()
			}

			def value = this."$prop"
			
			if(value){
				return value
			}
			
			Class type = target.getType()
			
			switch(type){
				case String:
					value = ""
					break
				case [Byte, Short, Integer, Long, Float, Double, BigInteger, BigDecimal]:
					value = 0
					break
				case Boolean:
					value = false
					break
			}
			return value
		}else{
			return method.call()
		}
	}
}
